package com.css.apps.base.user.action;

import com.css.apps.base.common.provider.json.OrgJsonProvider;
import com.css.apps.base.org.model.SOrg;
import com.css.apps.base.user.model.SUser;
import com.css.apps.base.user.po.UserInfoPageVo;
import com.css.apps.base.user.service.UserService;
import com.css.core.model.tree.Tree;
import com.css.core.model.tree.TreeCache;
import com.css.core.model.tree.TreeNode;
import com.css.db.query.QueryCache;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.opensymphony.xwork.Action;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("serial")
public class GetUserInfoPage extends UserAction {
	private static Log log = LogFactory.getLog(GetUserInfoPage.class);
	private String userId;
	private String lableId;
	private String pageName; //转发的用户数据信息页面名称
	private UserInfoPageVo userInfoPageVo;
	private String operFlag="add";
	private String tree="";
	private String viewMod;

	@SuppressWarnings("unchecked")
	public String userGo() {
		try {
			if(StringHelper.isNotEmpty(userId)){
				UserService userService = new UserService();
				userInfoPageVo = userService.createUserInfoPageVo(lableId, userId);
				operFlag = userService.getOperFlag();
			}
			if("userInfo".equals(lableId)){
				getOrgTree();
			}
			this.pageName = "readonly".equals(viewMod) ? lableId.toLowerCase() + "_readonly.jsp": lableId.toLowerCase() + ".jsp";
			return Action.SUCCESS;
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			setMessage(Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}

	/**
	 * 获取组织机构树
	 */
	private void getOrgTree(){
		Tree orgTree = new TreeCache(SOrg.class);
		OrgJsonProvider json = new OrgJsonProvider();
		if (StringHelper.isNotEmpty(userId)) {
			List<String> orgList = QueryCache.get(SUser.class, userId).getOrgList().getListById();
			userInfoPageVo.setOrgIds(StringHelper.listToStr(orgList));
			this.tree = json.getOrgCheckbox(orgList).toString();
		} else {
			TreeNode rootNode = orgTree.getRoot().get(0);
			if (StringHelper.isEmpty(userInfoPageVo.getOrgIds()) && (rootNode != null) && StringHelper.isNotEmpty(rootNode.getNodeId())) {
				userInfoPageVo.setOrgIds(rootNode.getNodeId());
			}
			List<String> orgList = new ArrayList<String>();
			orgList.add(userInfoPageVo.getOrgIds());
			this.tree = json.getOrgCheckbox(orgList).toString();
		}
	}

	public static Log getLog() {
		return log;
	}

	public static void setLog(Log log) {
		GetUserInfoPage.log = log;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getLableId() {
		return lableId;
	}

	public void setLableId(String lableId) {
		this.lableId = lableId;
	}

	public String getPageName() {
		return pageName;
	}

	public void setPageName(String pageName) {
		this.pageName = pageName;
	}

	public UserInfoPageVo getUserInfoPageVo() {
		return userInfoPageVo;
	}

	public void setUserInfoPageVo(UserInfoPageVo userInfoPageVo) {
		this.userInfoPageVo = userInfoPageVo;
	}

	public String getOperFlag() {
		return operFlag;
	}

	public void setOperFlag(String operFlag) {
		this.operFlag = operFlag;
	}

	public String getTree() {
		return tree;
	}

	public void setTree(String tree) {
		this.tree = tree;
	}

	public String getViewMod() {
		return viewMod;
	}

	public void setViewMod(String viewMod) {
		this.viewMod = viewMod;
	}
}
